<?php namespace app\api\controller;

use app\common\controller\AuthAndBind;
use think\Controller;
use think\Db;
use alisms\SendSms;
use esign\EsignHandle;

/**
 * api
 */
class Open extends Controller{
	
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 获取登录验证码
	 */
	public function getLoginCode(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['nickname'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$code = controller("service/Cache") -> getCache('nameLoginCode', $in['nickname']);
				
				$outArr['code'] = '1';
				$outArr['loginCode'] = $code;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取登录验证码',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 刷新登录验证码
	 */
	public function refreshLoginCode(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['nickname'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$newCode = rand(1000, 9999);
				controller("service/Cache") -> setCache('nameLoginCode', $in['nickname'], $newCode);
				
				$outArr['code'] = '1';
				$outArr['loginCode'] = $newCode;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'刷新登录验证码',
						'business_type'=>3,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 获取默认素材
	 */
	public function getDefaultMaterial(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty([], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$outArr['code'] = '1';
				$outArr['info']['avatar'] = config("QI_NIU")['domain'] . config("DEFAULT_MATERIAL")['AVATAR'];
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取默认素材',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}

	/**
	 * 公开获取店铺详情
	 */
	public function getShopDetails(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['shop_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				if( isset($in['shop_id']) ) $where[] = ['id', 'eq', $in['shop_id']];
				if( isset($in['status']) ) $where[] = ['status', 'eq', $in['status']];
				
				$data = Db::table("gd_shop") -> where($where) -> find();
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'公开获取店铺详情',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 活动详情
	 */
	public function getOperationDetails(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['operation_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$where = [];
				$where[] = ['id', 'eq', $in['operation_id']];
				
				$data = Db::table("gd_operation") -> where($where) -> find();
				
				if( !empty($data) ){
					$data['userNums'] = Db::table("gd_operation_user") -> where(['operation_id'=>$in['operation_id']]) -> count();
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				$old_data_json = [];
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取活动详情',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 活动列表
	 */
	public function getOperationList(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty([], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$num = isset($in['num']) ? $in['num'] : 20;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where = [];
//				$where[] = ['is_delete', 'eq', 0];
				if( isset($in['create_date']) ) $where[] = ['create_date', 'between', $in['create_date']];
				if( isset($in['start_date1']) && isset($in['end_date1']) ) {
					$where[] = ['start_date1', 'egt', $in['start_date1']];
					$where[] = ['end_date1', 'elt', $in['end_date1']];
				}
				
				$data = Db::table("gd_operation") -> where($where) -> order("id desc") -> limit($start, $num) -> select();
				$count = Db::table("gd_operation") -> where($where) -> count();

//				非正式版小程序
				if( isset($in['environment']) && !empty($in['environment']) && $in['environment'] != 'release' ){
					$data = [
						        [
						            "id"=> 9,
						            "name"=> "文创手串限时抢购",
						            "address"=> "元福明灯阁商城",
						            "head"=>"客服小二",
						            "head_phone"=> "13011111111",
						            "nums"=>200,
						            "start_date1"=> "2025-08-28 00:00:00",
						            "end_date1"=> "2025-08-30 23:59:59",
						            "start_date2"=> "2025-08-28 00:00:00",
						            "end_date2"=> "2025-08-30 23:59:59",
						            "pic"=> '[{"name":"活动-手串.png","type":"image\\/png","url":"https:\\/\\/cdn.longhudao.cn\\/202508121606521754986012221886.png"}]',
						            "introduce"=> '<p><img src="https://cdn.longhudao.cn/202508121607211754986041205386.jpg"></p>',
						            "create_user_id"=> 1,
						            "create_user_name"=> "admin",
						            "create_user_type"=> 1,
						            "create_date"=> "2025-08-12 16:07:26",
						            "userNums"=> 0
						        ]
						    ];
					$count = 1;
				}
				
				if( !empty($data) ){
					foreach ($data as $k => $v) {
						$data[$k]['userNums'] = Db::table("gd_operation_user") -> where(['operation_id'=>$v['id']]) -> count();
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				$old_data_json = [];
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取活动列表',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 获取公共配置项
	 */
	public function getCommonConfig(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty([], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$outArr['code'] = '1';
				$outArr['info'] = config("COMMON_CONFIG");
				
//				写入操作日志
				$old_data_json = [];
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取公共配置项',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 公开获取鉴定配置详情
	 */
	public function getIdentifyDetails(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$where[] = ['id', 'eq', $in['id']];
				
				$data = Db::table("gd_identify_config") -> where($where) -> find();
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'公开获取鉴定配置详情',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 公开获取店铺列表
	 */
	public function getShopList(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['status', 'state'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$num = isset($in['num']) ? $in['num'] : 0;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where = [];
				if( isset($in['status']) ) $where[] = ['status', 'eq', trim($in['status'])];
				if( isset($in['state']) ) $where[] = ['state', 'eq', trim($in['state'])];
				if( isset($in['create_user_id']) ) $where[] = ['create_user_id', 'eq', trim($in['create_user_id'])];
//				if( isset($in['searchGoodsName']) ) $where[] = ['name', 'like', '%' . trim($in['searchGoodsName']) . '%'];
//				if( isset($in['searchClassId1']) ) $where[] = ['class_id1', 'eq', $in['searchClassId1']];
//				if( isset($in['searchClassId2']) ) $where[] = ['class_id2', 'eq', $in['searchClassId2']];
//				if( isset($in['searchClassId3']) ) $where[] = ['class_id3', 'eq', $in['searchClassId3']];
				$orderBy = 'id desc';
//				if( isset($in['orderBy']) && !empty($in['orderBy']) ) {
//					$sort = explode('|', $in['orderBy']);
//					if( $sort[0] == 'curr_price' ) $orderBy = $sort[0] . ' ' . $sort[1];
//					if( $sort[0] == 'create_date' ) $orderBy = $sort[0] . ' ' . $sort[1];
//				}
				
				$data = Db::table("gd_shop")
												-> where($where) 
												-> order($orderBy) 
												-> limit($start, $num) 
												-> select();
				$count = Db::table("gd_shop")
												-> where($where) 
												-> count();
												
//				非正式版小程序
				if( isset($in['environment']) && !empty($in['environment']) && $in['environment'] != 'release' ){
					$data = [];
					$count = 0;
				}
												
				if( !empty($data) ){
					foreach ($data as $k => $v) {
						$data[$k]['goodsNums'] = Db::table("gd_goods") -> where(['shop_id'=>$v['id'], 'is_delete'=>0, 'status'=>1]) -> count();
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'公开获取店铺列表',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}

	/**
	 * 公开获取拍卖详情
	 */
	public function getAuctionDetails(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$num = isset($in['num']) ? $in['num'] : 0;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where = [];
				$where[] = ['is_delete', 'eq', 0];
				$where[] = ['id', 'eq', $in['id']];
				
				$data = Db::table("gd_auction") -> where($where) -> find();
				
				if( !empty($data) ){
					$data['bidprice'] = Db::table("gd_auction_bidprice") -> where(['auction_id'=>$data['id']]) -> order("id desc") -> select();
					$data['preach'] = Db::table("gd_preach") -> where(['auction_id'=>$data['id'], 'is_delete'=>0]) -> select();
				}
														
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'公开获取拍卖详情',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 公开获取拍卖列表
	 */
	public function getAuctionList(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty([], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$num = isset($in['num']) ? $in['num'] : 0;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where = [];
				$where[] = ['is_delete', 'eq', 0];
				$where[] = ['status', 'eq', 1];
				$where[] = ['verify_status', 'eq', 1];
				if( isset($in['type']) ) $where[] = ['a.type', 'eq', trim($in['type'])];
				if( isset($in['searchGoodsName']) ) $where[] = ['name', 'like', '%' . trim($in['searchGoodsName']) . '%'];
				if( isset($in['searchClassId1']) ) $where[] = ['class_id1', 'eq', $in['searchClassId1']];
				if( isset($in['searchClassId2']) ) $where[] = ['class_id2', 'eq', $in['searchClassId2']];
				if( isset($in['searchClassId3']) ) $where[] = ['class_id3', 'eq', $in['searchClassId3']];
				$orderBy = 'id desc';
				if( isset($in['orderBy']) && !empty($in['orderBy']) ) {
					$sort = explode('|', $in['orderBy']);
					if( $sort[0] == 'curr_price' ) $orderBy = $sort[0] . ' ' . $sort[1];
					if( $sort[0] == 'create_date' ) $orderBy = $sort[0] . ' ' . $sort[1];
				}
				
				$data = Db::table("gd_auction")
												-> where($where) 
												-> order($orderBy) 
												-> limit($start, $num) 
												-> select();
				$count = Db::table("gd_auction")
												-> where($where) 
												-> count();
												
				if( !empty($data) ){
					foreach ($data as $k => $v) {
						if( $v['start_date'] <= date("Y-m-d H:i:s", time()) && $v['end_date2'] >= date("Y-m-d H:i:s", time()) ){
							$data[$k]['isInProgress'] = '进行中';
						}
						if( $v['start_date'] > date("Y-m-d H:i:s", time()) ){
							$data[$k]['isInProgress'] = '未开始';
						}
						if( $v['end_date2'] < date("Y-m-d H:i:s", time()) ){
							$data[$k]['isInProgress'] = '已结束';
						}
					}
				}
														
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'公开获取拍卖列表',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 公开获取新品/精品/捡漏列表
	 */
	public function getCollectionGoodsList(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty([], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$num = isset($in['num']) ? $in['num'] : 0;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where = [];
				$where[] = ['c.is_delete', 'eq', 0];
				$where[] = ['b.is_delete', 'eq', 0];
				$where[] = ['c.status', 'eq', 1];
				if( isset($in['type']) ) $where[] = ['a.type', 'eq', trim($in['type'])];
				if( isset($in['searchGoodsName']) ) $where[] = ['c.name', 'like', '%' . trim($in['searchGoodsName']) . '%'];
				if( isset($in['searchClassId1']) ) $where[] = ['c.class_id1', 'eq', $in['searchClassId1']];
				if( isset($in['searchClassId2']) ) $where[] = ['c.class_id2', 'eq', $in['searchClassId2']];
				if( isset($in['searchClassId3']) ) $where[] = ['c.class_id3', 'eq', $in['searchClassId3']];
				$orderBy = 'a.id desc';
				if( isset($in['orderBy']) && !empty($in['orderBy']) ) {
					$sort = explode('|', $in['orderBy']);
					if( $sort[0] == 'sort' ) $orderBy = 'c.' . $sort[0] . ' ' . $sort[1];
					if( $sort[0] == 'price' ) $orderBy = 'b.' . $sort[0] . ' ' . $sort[1];
					if( $sort[0] == 'create_date' ) $orderBy = 'a.' . $sort[0] . ' ' . $sort[1];
				}
				
//				非正式版小程序
				if( isset($in['environment']) && !empty($in['environment']) && $in['environment'] != 'release' ){
					$where[] = ['c.id', 'eq', 1329];
				}
				
				$field = ['a.*', 'c.main_image', 'c.name'=>'goods_name', 'a.id'=>'collection_goods_id', 'c.details'];
				$data = Db::table("gd_collection_goods") -> alias('a')
														-> join('gd_goods_spec b', 'a.goods_id = b.goods_id', 'left') 
														-> join('gd_goods c', 'a.goods_id = c.id', 'left') 
														-> field($field) 
														-> where($where) 
														-> order($orderBy) 
														-> group("a.id") 
														-> limit($start, $num) 
														-> select();
				$count = Db::table("gd_collection_goods") -> alias('a')
														-> join('gd_goods_spec b', 'a.goods_id = b.goods_id', 'left') 
														-> join('gd_goods c', 'a.goods_id = c.id', 'left') 
														-> where($where) 
														-> group("a.id") 
														-> count();
														
				if( !empty($data) ){
					$userLevel = [];
					if( isset($in['search_user_id']) && $in['search_user_id'] > 0 ){
//						获取用户等级
						$userLevel = controller("service/User") -> getUserLevelInfo($in['search_user_id']);
					}
					$levelId = !empty($userLevel) ? $userLevel['id'] : 0;
					foreach ($data as $k => $v) {
						$data[$k]['is_period'] = FALSE;		//藏趣活动商品是否在活动有效期内
						if( $v['start_date'] < date("Y-m-d H:i:s", time()) && $v['end_date'] > date("Y-m-d H:i:s", time()) ) $data[$k]['is_period'] = TRUE;
						
//						商品正常最低价
						$data[$k]['min_price'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$v['goods_id'], 'is_delete'=>0]) -> min("price");
//						商品规格组
                        $data[$k]['goods_spec'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$v['goods_id'], 'is_delete'=>0]) -> select();
                        
//						用户等级id
                        $data[$k]['level_id'] = $levelId;
//						等级最低价
						$data[$k]['level_min_price'] = $data[$k]['min_price'];
	                    if( !empty($userLevel)){
	                    	$goodsSpecLevelPrice = Db::table("gd_goods_spec_level_price") -> where(['goods_id'=>$v['goods_id'], 'level_id'=>$levelId, 'is_delete'=>0]) -> order("price asc") -> find();
	                    	$goodsSpecLevelPrice = strval($goodsSpecLevelPrice['price']);
	                    	
	                    	if( !empty($goodsSpecLevelPrice) ) $data[$k]['level_min_price'] = $goodsSpecLevelPrice;
	                    }
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'公开获取新品/精品/捡漏列表',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}

	/**
	 * 公开获取商品详情(商品上架凭证)
	 */
	public function getGoodsDetails3(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['goods_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$where = [];
				$where[] = ['is_delete', 'eq', 0];
				$where[] = ['id', 'eq', $in['goods_id']];
				
				$data['goods'] = Db::table("gd_goods") -> where($where) -> find();
				
				if( !empty($data['goods']) ){
					$data['calss3IdentifyFee'] = Db::table("gd_classification") -> where(['id'=>$data['goods']['class_id3']]) -> find()['identify_fee'];
					$data['goodsSpec'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$data['goods']['id'], 'is_delete'=>0]) -> select();
					$data['min_price'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$data['goods']['id'], 'is_delete'=>0]) -> min("price");
//					$data['total_nums'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$data['goods']['id'], 'is_delete'=>0]) -> sum("num");
					$data['total_nums'] = 0;
					$data['is_period'] = FALSE;
					if( isset($in['collection_goods_id']) ) {
						$data['collection_goods'] = Db::table("gd_collection_goods") -> where(['id'=>$in['collection_goods_id']]) -> find();
						if( $data['collection_goods']['start_date'] < date("Y-m-d H:i:s", time()) && $data['collection_goods']['end_date'] > date("Y-m-d H:i:s", time()) ) $data['is_period'] = TRUE;
					}
					$data['preach'] = Db::table("gd_preach") -> where(['goods_id'=>$data['goods']['id'], 'is_delete'=>0]) -> select();
					
//					当前价格
					$data['goods_subgoods'] = Db::table("gd_goods_subgoods") -> where(['sub_goods_id'=>$in['goods_id'], 'is_delete'=>0]) -> find();
					$data['historical_sales_quantity'] = Db::table("gd_order") -> where(['goods_id'=>$data['goods']['id']]) -> sum("num");
					
//					他人分享
					if( isset($in['parent_user_id']) && $in['parent_user_id'] > 0 && $in['parent_user_id'] != $in['user_id'] ) {
//						可购买数量
						$order_subgoods = Db::table("gd_order_subgoods") -> alias('a')
																		-> join('gd_order b', 'a.order_id = b.id', 'left')
																		-> field(['sum(a.nums)'=>'nums','sum(a.buy_nums)'=>'buy_nums', 'sum(a.nums)-sum(a.buy_nums)'])
																		-> where([['a.goods_id', 'eq', $in['goods_id']], 
																				  ['b.create_user_id', 'eq', $in['parent_user_id']], 
																				  ['b.status', 'in', [1, 12]]]) -> find();
						if( !empty($order_subgoods) ) $data['total_nums'] = $order_subgoods['sum(a.nums)-sum(a.buy_nums)'];
						$data['goods_subgoods']['price1'] = $data['goods_subgoods']['price2'];
						$data['goods_subgoods']['price2'] = $data['goods_subgoods']['price2'];
						$data['goodsSpec'][0]['price'] = $data['goods_subgoods']['price2'];
						$data['goodsSpec'][0]['num'] = $data['total_nums'];
					}
//					自己购买主商品
					if( !empty($in['user_id']) && ($in['parent_user_id'] == 0 || $in['parent_user_id'] == $in['user_id']) ) {
//						可购买数量
						$order_subgoods = Db::table("gd_order_subgoods") -> alias('a')
																		-> join('gd_order b', 'a.order_id = b.id', 'left')
																		-> field(['sum(a.nums)'=>'nums','sum(a.buy_nums)'=>'buy_nums', 'sum(a.nums)-sum(a.buy_nums)'])
																		-> where([['a.goods_id', 'eq', $in['goods_id']], 
																				  ['b.create_user_id', 'eq', $in['user_id']], 
																				  ['b.status', 'in', [1, 12]]]) -> find();
						if( !empty($order_subgoods) ) $data['total_nums'] = $order_subgoods['sum(a.nums)-sum(a.buy_nums)'];
						$data['goods_subgoods']['price1'] = $data['goods_subgoods']['price1'];
						$data['goods_subgoods']['price2'] = $data['goods_subgoods']['price1'];
						$data['goodsSpec'][0]['price'] = $data['goods_subgoods']['price1'];
						$data['goodsSpec'][0]['num'] = $data['total_nums'];
					}
					
					$data['shopping_history'] = Db::table("gd_order") -> alias('a')
																		-> join('gd_user b', 'a.create_user_id = b.id', 'left') 
																		-> field(['a.goods_name', 'a.total_price', 'a.status', 'a.create_date'=>'order_create_date', 'b.nickname2', 'b.avatar']) 
																		-> where(['goods_id'=>$data['goods']['id']]) -> select();
					$data['goods_delivery_method'] = Db::table("gd_goods_delivery_method") -> where(['goods_id'=>$data['goods']['id'], 'is_delete'=>0]) -> order("id desc") -> select();
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'公开获取商品详情',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 获取指定用户等级
	 */
	private function getUserLevel($userId){
		$userInfo = Db::table("gd_user") -> where(['id'=>$userId]) -> find();
//		有等级ID，并且在有效期
		$date = date("Y-m-d H:i:s", time());
		if( $userInfo['level_config_id'] > 0 && $date > $userInfo['level_start_date'] && $date < $userInfo['level_end_date'] ){
			$levelId = $userInfo['level_config_id'];
		}else{
//			获取用户角色的等级id
			$where[] = ['a.uid', 'eq', $userId];
			$where[] = ['b.is_delete', 'eq', 0];
			$authGroup = Db::table("gd_auth_group_access") -> alias('a') -> join('gd_auth_group b', 'a.group_id = b.id', 'right') -> field(['b.*']) -> where($where) -> find();
			$levelId = empty($authGroup) ? 0 : $authGroup['level_config_id'];
		}
		return $levelId;
	}
	
	/**
	 * 获取制定商品商家进货的库存
	 */
	private function mergeInventory($goodsSpec, $goodsId){
		$where[] = ['parent_id', 'in', array_column($goodsSpec, 'id')];
		$where[] = ['is_delete', 'eq', 0];
		$spec = Db::table("gd_goods_spec") -> where($where) -> select();
		
		if( !empty($spec) ){
			foreach($goodsSpec as $k => $v){
				foreach($spec as $kk => $vv){
					if( $v['id'] == $vv['parent_id'] ) $goodsSpec[$k]['num'] = $v['num'] + $vv['num'];
				}
			}
		}
		return $goodsSpec;
	}
	
	/**
	 * 公开获取商品详情
	 */
	public function getGoodsDetails(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['goods_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$num = isset($in['num']) ? $in['num'] : 0;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where = [];
				$where[] = ['is_delete', 'eq', 0];
				$where[] = ['id', 'eq', $in['goods_id']];
				
				$data['goods'] = Db::table("gd_goods") -> where($where) -> find();
				
				if( !empty($data['goods']) ){
					$data['calss3IdentifyFee'] = Db::table("gd_classification") -> where(['id'=>$data['goods']['class_id3']]) -> find()['identify_fee'];
					
					$data['goodsSpec'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$data['goods']['id'], 'is_delete'=>0]) -> select();			//主商品规格
					$data['min_price'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$data['goods']['id'], 'is_delete'=>0]) -> min("price");		//最小价保
					$data['total_nums'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$data['goods']['id'], 'is_delete'=>0]) -> sum("num");		//主商品总库存
					
					$userLevel = [];
					if( isset($in['search_user_id']) && $in['search_user_id'] > 0 ){
//						获取用户等级
						$userLevel = controller("service/User") -> getUserLevelInfo($in['search_user_id']);
					}
//					用户等级id
                    $data['level_id'] = !empty($userLevel) ? $userLevel['id'] : 0;
//					等级最低价
					$data['level_min_price'] = $data['min_price'];
                    if( !empty($userLevel)){
                    	$goodsSpecLevelPrice = Db::table("gd_goods_spec_level_price") -> where(['goods_id'=>$data['goods']['id'], 'level_id'=>$data['level_id'], 'is_delete'=>0]) -> order("price asc") -> find();
                    	$goodsSpecLevelPrice = strval($goodsSpecLevelPrice['price']);
                    	
                    	if( !empty($goodsSpecLevelPrice) ) $data['level_min_price'] = $goodsSpecLevelPrice;
                    }
					
//					合并计算库存,读取合并商家进货后的库存
					if( isset($in['isMergeInventory']) && $in['isMergeInventory'] == 1 ){
						$data['goodsSpec'] = $this -> mergeInventory($data['goodsSpec'], $data['goods']['id']);										//商家合并子商品总库存
						$data['total_nums'] = array_sum(array_column($data['goodsSpec'], 'num'));
					}
//					去除进货的销量
					if( isset($in['isRemoveIncomingGoods']) && $in['isRemoveIncomingGoods'] == 1 ){
						$data['historical_sales_quantity'] = Db::table("gd_order") -> where([['goods_id|parent_goods_id', 'eq', $data['goods']['id']], ['source', 'neq', 21]]) -> sum("num");				//主商品已售数量
					}else{
						$data['historical_sales_quantity'] = Db::table("gd_order") -> where(['goods_id'=>$data['goods']['id']]) -> sum("num");				//主商品已售数量
					}
					
					
					$data['is_period'] = FALSE;
					if( isset($in['collection_goods_id']) ) {
						$data['collection_goods'] = Db::table("gd_collection_goods") -> where(['id'=>$in['collection_goods_id']]) -> find();
						if( $data['collection_goods']['start_date'] < date("Y-m-d H:i:s", time()) && $data['collection_goods']['end_date'] > date("Y-m-d H:i:s", time()) ) $data['is_period'] = TRUE;
					}
					$data['preach'] = Db::table("gd_preach") -> where(['goods_id'=>$data['goods']['id'], 'is_delete'=>0]) -> select();
					if( isset($in['order_id']) && $in['order_id'] > 0 ) {
						$data['orderSubgoods'] = Db::table("gd_order_subgoods") -> where(['order_id'=>$in['order_id'], 'goods_id'=>$in['goods_id']])  -> find();
					}
					
					$shoppingHistoryWhere[] = ['goods_id|parent_goods_id', 'eq', $data['goods']['id']];
					if( isset($in['isRemoveIncomingGoods']) && $in['isRemoveIncomingGoods'] == 1 ) $shoppingHistoryWhere[] = ['source', 'neq', 21];
					$data['shopping_history'] = Db::table("gd_order") -> alias('a')
																		-> join('gd_user b', 'a.create_user_id = b.id', 'left') 
																		-> field(['a.goods_name', 'a.total_price', 'a.status', 'a.create_date'=>'order_create_date', 'b.nickname2', 'b.avatar']) 
																		-> where($shoppingHistoryWhere) -> select();
					$data['goods_delivery_method'] = Db::table("gd_goods_delivery_method") -> where(['goods_id'=>$data['goods']['id'], 'is_delete'=>0]) -> order("id desc") -> select();
					$data['isCollect'] = FALSE;
					if( isset($in['user_id']) && $in['user_id'] > 0 ) {
						$isCollect = Db::table("gd_collect") -> where(['goods_id'=>$data['goods']['id'], 'create_user_id'=>$in['user_id']]) -> find();
						if( !empty($isCollect) ) $data['isCollect'] = TRUE;
					}
					
					if( isset($in['user_id']) && $in['user_id'] > 0 && !empty($data['goodsSpec']) ){
//						用户已登录，获取该用户等级ID
						$userLevelId = $this -> getUserLevel($in['user_id']);
						foreach ($data['goodsSpec'] as $k => $v) {
							$goodsSpecLevelPriceWhere = [];
							$goodsSpecLevelPriceWhere[] = ['goods_id', 'eq', $data['goods']['id']];
							$goodsSpecLevelPriceWhere[] = ['goods_spec_id', 'eq', $v['id']];
							$goodsSpecLevelPriceWhere[] = ['level_id', 'eq', $userLevelId];
							$userLevelPrice = Db::table("gd_goods_spec_level_price") -> where($goodsSpecLevelPriceWhere) -> find();
							if( !empty($userLevelPrice) ) $data['goodsSpec'][$k]['price'] = $userLevelPrice['price'] != null ? $userLevelPrice['price'] : $v['price'];
						}
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'公开获取商品详情',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}

	/**
	 * 公开获取商品列表
	 */
	public function getGoodsList(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty([], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$num = isset($in['num']) ? $in['num'] : 0;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where = [];
				$where[] = ['is_delete', 'eq', 0];
				if( isset($in['searchGoodsName']) ) $where[] = ['name', 'like', '%' . trim($in['searchGoodsName']) . '%'];
				if( isset($in['shop_id']) ) $where[] = ['shop_id', 'eq', trim($in['shop_id'])];
				if( isset($in['status']) ) $where[] = ['status', 'eq', trim($in['status'])];
				if( isset($in['goods_type']) ) $where[] = ['goods_type', 'in', $in['goods_type']];
				if( isset($in['isFilterSpecialGoods']) && $in['isFilterSpecialGoods'] == 1 ) $where[] = ['goods_type', 'eq', 1];
				
				$data = Db::table("gd_goods") -> where($where) -> order("id desc") -> limit($start, $num) -> select();
				$count = Db::table("gd_goods") -> where($where) -> count();
				
//				非正式版小程序
				if( isset($in['environment']) && !empty($in['environment']) && $in['environment'] != 'release' ){
					$data = [
						        [
						            "id"=> 1329,
						            "name"=> "五行手持",
						            "title"=> "",
						            "is_be_commissioned_to_sell_sth"=> 0,
						            "is_identify"=> 0,
						            "is_free_shipping"=> 1,
						            "carriage"=> "0.00",
						            "main_image"=> '[{"name":"202508081540561754638856822479.jpg","type":"image\\/jpeg","url":"https:\\/\\/cdn.longhudao.cn\\/202508290150451756403445935509.jpg"}]',
						            "delivery_time"=> 2,
						            "label"=> "[]",
						            "class_id1"=> 11,
						            "class_id2"=> 0,
						            "class_id3"=> 0,
						            "details"=> '<p><img src="https://cdn.longhudao.cn/202508290152021756403522831481.jpg"></p>',
						            "status"=> 1,
						            "goods_type"=> 1,
						            "shop_id"=> 0,
						            "sort"=> 1,
						            "rebate_type"=> 0,
						            "rebate"=> "0.00",
						            "parent_id"=> 0,
						            "is_delete"=> 0,
						            "create_user_id"=> 1,
						            "create_user_name"=> "admin",
						            "create_user_type"=> 1,
						            "create_date"=> "2025-08-29 01:52:06",
						            "totalInventory"=> 998,
						            "min_price_protection"=> 999,
						            "max_price_protection"=> 999,
						            "min_price"=> 999,
						            "calssName"=> null
						        ]
						    ];
					$count = 1;
				}
				
				if( !empty($data) ){
//					if( isset($in['village_id']) && $in['village_id'] > 0 ){
//						$villageGoods = Db::table("gd_village_goods") -> where(['village_id'=>$in['village_id']]) -> select();
//					}
//					if( isset($in['collection_type']) && $in['collection_type'] > 0 ){
//						$collectionGoods = Db::table("gd_collection_goods") -> where(['type'=>$in['collection_type']]) -> select();
//					}
					
					$userLevel = [];
					if( isset($in['search_user_id']) && $in['search_user_id'] > 0 ){
//						获取用户等级
						$userLevel = controller("service/User") -> getUserLevelInfo($in['search_user_id']);
					}
					$levelId = !empty($userLevel) ? $userLevel['id'] : 0;
					foreach ($data as $k => $v) {
						$data[$k]['totalInventory'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$v['id'], 'is_delete'=>0]) -> sum("num");
						$data[$k]['min_price_protection'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$v['id'], 'is_delete'=>0]) -> min("price");
						$data[$k]['max_price_protection'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$v['id'], 'is_delete'=>0]) -> max("price");
						$data[$k]['min_price'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$v['id'], 'is_delete'=>0]) -> min("price");
						if( !empty($v['class_id1']) ) $data[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id1']]) -> find()['name'];
						if( !empty($v['class_id2']) ) $data[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id2']]) -> find()['name'];
						if( !empty($v['class_id3']) ) $data[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id3']]) -> find()['name'];
						
						
//						用户等级id
                        $data[$k]['level_id'] = $levelId;
//						等级最低价
						$data[$k]['level_min_price'] = $data[$k]['min_price'];
	                    if( !empty($userLevel)){
	                    	$goodsSpecLevelPrice = Db::table("gd_goods_spec_level_price") -> where(['goods_id'=>$v['goods_id'], 'level_id'=>$levelId, 'is_delete'=>0]) -> order("price asc") -> find();
	                    	$goodsSpecLevelPrice = strval($goodsSpecLevelPrice['price']);
	                    	
	                    	if( !empty($goodsSpecLevelPrice) ) $data[$k]['level_min_price'] = $goodsSpecLevelPrice;
	                    }
						
//						村落
//						if( isset($in['village_id']) && $in['village_id'] > 0 ){
//							$data[$k]['isVillageGoods'] = FALSE;
//							if( isset($villageGoods) && !empty($villageGoods) ){
//								foreach ($villageGoods as $kk => $vv) {
//									if( $v['id'] == $vv['goods_id'] ) $data[$k]['isVillageGoods'] = TRUE;
//								}
//							}
//						}
//						藏趣
//						if( isset($in['collection_type']) && $in['collection_type'] > 0 ){
//							$data[$k]['isCollectionGoods'] = FALSE;
//							if( isset($collectionGoods) && !empty($collectionGoods) ){
//								foreach ($collectionGoods as $kk => $vv) {
//									if( $v['id'] == $vv['goods_id'] ) $data[$k]['isCollectionGoods'] = TRUE;
//								}
//							}
//						}
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'公开获取商品列表',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 公开获取分类列表
	 */
	public function getClassificationList(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty([], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$num = isset($in['num']) ? $in['num'] : 0;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where = [];
				$where[] = ['is_delete', 'eq', 0];
//				if( isset($in['parent_id']) ) $where[] = ['parent_id', 'eq', $in['parent_id']];
				if( isset($in['is_delete']) ) $where[] = ['is_delete', 'eq', trim($in['is_delete'])];
				if( isset($in['level']) ) $where[] = ['level', 'eq', trim($in['level'])];
				
				$data = Db::table("gd_classification") -> where($where) -> order("id desc") -> limit($start, $num) -> select();
				$count = Db::table("gd_classification") -> where($where) -> count();
				
				if( !empty($data) ) {
					foreach ($where as $k => $v) {
						if( $v[0] == 'level' ) unset($where[$k]);
					}
					$sonData = Db::table("gd_classification") -> where($where) -> select();
					foreach ($sonData as $k => $v) {
						$sonData[$k]['parent_name'] = '';
						if( $v['parent_id'] > 0 ){
							foreach ($sonData as $kk => $vv) {
								if( $v['parent_id'] == $vv['id'] ) $sonData[$k]['parent_name'] = $vv['name'];
							}
						}
					}
					$sonData = $this -> tree($sonData);
					foreach ($data as $k => $v) {
						foreach ($sonData as $kk => $vv) {
							if( $v['id'] == $vv['id'] ) $data[$k] = $vv;
						}
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'公开获取村落列表',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}

	/**
	 * 树形结构
	 */	
	public function tree($data, $pk = 'id', $pid = 'parent_id', $child = 'children', $root = 0){
//		创建Tree
		$tree = [];
		if (!is_array($data)) {
		    return false;
		}
          
//		创建基于主键的数组引用
        $refer = [];
        foreach ($data as $key => $value_data) {
            $refer[$value_data[$pk]] = &$data[$key];
        }
        foreach ($data as $key => $value_data) {
//			判断是否存在parent
            $parentId = $value_data[$pid];
            if ($root == $parentId) {
                $tree[] = &$data[$key];
            } else {
                if (isset($refer[$parentId])) {
                    $parent = &$refer[$parentId];
                    $parent[$child][] = &$data[$key];
                }
            }
        }
        return $tree;
	}
	
	/**
	 * 公开获取鉴定列表
	 */
	public function getIdentifyList(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty([], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$num = isset($in['num']) ? $in['num'] : 20;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where = [];
				$where[] = ['a.status', 'eq', 1];
				if( isset($in['goods_id']) ) {
					if( $in['goods_id'] == '>0' ) $where[] = ['a.goods_id', 'gt', 0];
				}

				if( isset($in['searchGoodsName']) ) $where[] = ['c.name', 'like', '%' . trim($in['searchGoodsName']) . '%'];
				if( isset($in['searchClassId1']) ) $where[] = ['c.class_id1', 'eq', $in['searchClassId1']];
				if( isset($in['searchClassId2']) ) $where[] = ['c.class_id2', 'eq', $in['searchClassId2']];
				if( isset($in['searchClassId3']) ) $where[] = ['c.class_id3', 'eq', $in['searchClassId3']];
				$orderBy = 'c.sort asc';
				if( isset($in['orderBy']) && !empty($in['orderBy']) ) {
					$sort = explode('|', $in['orderBy']);
					if( $sort[0] == 'sort' ) $orderBy = 'c.' . $sort[0] . ' ' . $sort[1];
					if( $sort[0] == 'assess' ) $orderBy = 'a.' . $sort[0] . ' ' . $sort[1];
					if( $sort[0] == 'create_date' ) $orderBy = 'a.' . $sort[0] . ' ' . $sort[1];
				}
				
				$field = ['a.*', 'c.name'=>'goods_name', 'c.main_image'=>'goods_main_image'];
				$data = Db::table("gd_identify") -> alias('a')
//														-> join('gd_goods_spec b', 'a.goods_id = b.goods_id', 'left') 
														-> join('gd_goods c', 'a.goods_id = c.id', 'left') 
														-> field($field)
														-> where($where) 
														-> order($orderBy) 
														-> limit($start, $num) 
														-> select();
				$count = Db::table("gd_identify") -> alias('a')
//														-> join('gd_goods_spec b', 'a.goods_id = b.goods_id', 'left') 
														-> join('gd_goods c', 'a.goods_id = c.id', 'left') 
														-> where($where) 
														-> count();
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'公开获取鉴定列表',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}

	/**
	 * 公开获取村落商品列表
	 */
	public function getVillageGoodsList(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty([], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$num = isset($in['num']) ? $in['num'] : 20;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where = [];
				$where[] = ['c.is_delete', 'eq', 0];
				$where[] = ['b.is_delete', 'eq', 0];
				$where[] = ['c.status', 'eq', 1];
				if( isset($in['village_id']) ) $where[] = ['a.village_id', 'eq', trim($in['village_id'])];
				if( isset($in['searchGoodsName']) ) $where[] = ['c.name', 'like', '%' . trim($in['searchGoodsName']) . '%'];
				if( isset($in['searchClassId1']) ) $where[] = ['c.class_id1', 'eq', $in['searchClassId1']];
				if( isset($in['searchClassId2']) ) $where[] = ['c.class_id2', 'eq', $in['searchClassId2']];
				if( isset($in['searchClassId3']) ) $where[] = ['c.class_id3', 'eq', $in['searchClassId3']];
				$orderBy = 'c.sort asc';
				if( isset($in['orderBy']) && !empty($in['orderBy']) ) {
					$sort = explode('|', $in['orderBy']);
					if( $sort[0] == 'sort' ) $orderBy = 'c.' . $sort[0] . ' ' . $sort[1];
					if( $sort[0] == 'price' ) $orderBy = 'b.' . $sort[0] . ' ' . $sort[1];
					if( $sort[0] == 'create_date' ) $orderBy = 'c.' . $sort[0] . ' ' . $sort[1];
				}
				
				$field = ['c.main_image', 'c.name', 'c.title', 'a.goods_id'];
				$data = Db::table("gd_village_goods") -> alias('a')
														-> join('gd_goods_spec b', 'a.goods_id = b.goods_id', 'left') 
														-> join('gd_goods c', 'a.goods_id = c.id', 'left') 
														-> where($where) 
														-> order($orderBy) 
														-> group("a.goods_id") 
														-> limit($start, $num) 
														-> select();
				$count = Db::table("gd_village_goods") -> alias('a')
														-> join('gd_goods_spec b', 'a.goods_id = b.goods_id', 'left') 
														-> join('gd_goods c', 'a.goods_id = c.id', 'left') 
														-> where($where) 
														-> group("a.goods_id") 
														-> count();
				if( !empty($data) ){
					$userLevel = [];
					if( isset($in['search_user_id']) && $in['search_user_id'] > 0 ){
//						获取用户等级
						$userLevel = controller("service/User") -> getUserLevelInfo($in['search_user_id']);
					}
					$levelId = !empty($userLevel) ? $userLevel['id'] : 0;
					foreach ($data as $k => $v) {
						$data[$k]['min_price'] = Db::table("gd_goods_spec") -> where(['goods_id'=>$v['goods_id'], 'is_delete'=>0]) -> min("price");
						$data[$k]['level_id'] = $levelId;
						$data[$k]['level_min_price'] = $levelId > 0 ? Db::table("gd_goods_spec_level_price") -> where(['goods_id'=>$v['goods_id'], 'level_id'=>$levelId, 'is_delete'=>0]) -> min("price") : $data[$k]['min_price'];
						
//						等级最低价
						$data[$k]['level_min_price'] = $data[$k]['min_price'];
	                    if( !empty($userLevel)){
	                    	$goodsSpecLevelPrice = Db::table("gd_goods_spec_level_price") -> where(['goods_id'=>$v['goods_id'], 'level_id'=>$levelId, 'is_delete'=>0]) -> order("price asc") -> find();
	                    	$goodsSpecLevelPrice = strval($goodsSpecLevelPrice['price']);
	                    	
	                    	if( !empty($goodsSpecLevelPrice) ) $data[$k]['level_min_price'] = $goodsSpecLevelPrice;
	                    }
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'公开获取村落商品列表',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 公开获取村落列表
	 */
	public function getVillageList(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty([], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
				$num = isset($in['num']) ? $in['num'] : 20;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where = [];
				$where[] = ['is_delete', 'eq', 0];
				if( isset($in['create_date']) ) $where[] = ['create_date', 'between', $in['create_date']];
				if( isset($in['name']) ) $where[] = ['name', 'like', '%' . trim($in['name']) . '%'];
				if( isset($in['is_delete']) ) $where[] = ['is_delete', 'eq', trim($in['is_delete'])];
				
				$data = Db::table("gd_village") -> where($where) -> order("sort asc") -> limit($start, $num) -> select();
				$count = Db::table("gd_village") -> where($where) -> count();
				
				if( isset($in['environment']) && !empty($in['environment']) && $in['environment'] != 'release' ){
					$data = [];
					$count = 0;
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'公开获取村落列表',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}

	
	
	/**
	 * 公开获取商城配置项详情
	 */
	public function getShopConfigDetails(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty([], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$where[] = ['id', 'eq', 1];
				$data = Db::table("gd_shop_config") -> where($where) -> find();
				
				if( isset($in['environment']) && !empty($in['environment']) && $in['environment'] != 'release' ){
					$data = [
						"id"=> 1,
				        "pic"=> '["https:\\/\\/cdn.longhudao.cn\\/202507142025401752495940933862.png"]',
				        "create_user_id"=> 1,
				        "create_user_name"=> "admin",
				        "create_user_type"=> 2,
				        "create_date"=> "2024-10-17 11:20:58"
					];
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'公开获取商城配置项详情',
						'business_type'=>4,
						'oper_name'=>'',
						'oper_user_id'=>0,
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
}

?>